/**
 * 
 */
package display;

import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;

import main.Agent;
import main.Agent.AgentName;
import main.Grid;
import main.Main;

import display.MainGamePanel.AgentColor;
/**
 * Listens to combo boxes in the game panel
 * @author ES
 */
public class ComboBoxListener implements ActionListener {

	private JComboBox comboBox;
	private AgentColor agentColor;
	
	@SuppressWarnings("serial")
	/**
	 * Dialog box for setting the min max level
	 */
	class MinMaxDialog extends JDialog{
		JComboBox minmaxlevelbox;
		JButton okButton;
		Agent agent;
		MinMaxDialog(Frame owner, Agent ragent){
			super(owner);
			Integer[] items = {1,2,3,4,5};
			minmaxlevelbox = new JComboBox(items);
			minmaxlevelbox.setSelectedIndex(0);
			
			okButton = new JButton("OK");
			this.agent = ragent;
			okButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					agent.setMinMaxLevel((Integer)minmaxlevelbox.getSelectedItem());
					keelit();
				}
			});
			this.setTitle("Set Min Max Depth Level");
			this.setLayout(new BorderLayout());
			this.add(minmaxlevelbox, BorderLayout.CENTER);
			this.add(okButton, BorderLayout.SOUTH);
			
			this.setSize(250, 100);
			this.setLocation(200,200);
			this.setVisible(true);
			
		}
		
		/**
		 * Disposes this combo box
		 */
		public void keelit(){
			this.dispose();
		}
	}
	
	@SuppressWarnings("serial")
	/**
	 * Priority Agent Dialog box for picking the heuristic 
	 *
	 */
	class PriorityDialog extends JDialog{
		JComboBox priorityComboBox;
		JButton okButton;
		Agent agent;

		public PriorityDialog(Frame owner, Agent ragent) {
			super(owner);
			priorityComboBox = new JComboBox(Grid.Heuristic.values());
			priorityComboBox.setSelectedIndex(0);

			okButton = new JButton("OK");
			this.agent = ragent;
			okButton.addActionListener(new ActionListener() {
				@Override
				public void actionPerformed(ActionEvent arg0) {
					agent.setHeuristic((Grid.Heuristic)priorityComboBox.getSelectedItem());
					keelit();
				}
			});
			this.setTitle("Set Min Max Depth Level");
			this.setLayout(new BorderLayout());
			this.add(priorityComboBox, BorderLayout.CENTER);
			this.add(okButton, BorderLayout.SOUTH);

			this.setSize(250, 100);
			this.setLocation(200,200);
			this.setVisible(true);
		}

		/**
		 * Disposes this combo box
		 */
		public void keelit(){
			this.dispose();
		}
	}

	/**
	 * Creates a new combo box listener
	 * @param comboBox : The box to listen to 
	 * @param agentColor : The player color of the agent
	 */
	public ComboBoxListener(JComboBox comboBox, AgentColor agentColor)
	{
		setComboBox(comboBox);
		setAgentColor(agentColor);
	}
	
	@Override
	public void actionPerformed(ActionEvent arg0) {
		AgentName agentName = (AgentName)comboBox.getSelectedItem();
		if(agentColor.equals(AgentColor.BLACK)) //set black agent's name
		{
			
			MainGamePanel.GetInstance().getBlackAgent().setName(agentName);
			System.out.println("Black Player selected is: " + MainGamePanel.GetInstance().getBlackAgent().getName());
			if(agentName == AgentName.MINMAX || agentName == AgentName.WEIGHTED_MINMAX){
				new MinMaxDialog(Main.displayEngine, MainGamePanel.GetInstance().getBlackAgent());
			}else if(agentName == AgentName.PRIORITY){
				new PriorityDialog(Main.displayEngine, MainGamePanel.GetInstance().getBlackAgent());
			}
		}
		else //Set white agent's name
		{
			MainGamePanel.GetInstance().getWhiteAgent().setName(agentName);
			System.out.println("White Player selected is: " + MainGamePanel.GetInstance().getWhiteAgent().getName());
			if(agentName == AgentName.MINMAX || agentName == AgentName.WEIGHTED_MINMAX){
				new MinMaxDialog(Main.displayEngine, MainGamePanel.GetInstance().getWhiteAgent());
			}else if(agentName == AgentName.PRIORITY){
				new PriorityDialog(Main.displayEngine, MainGamePanel.GetInstance().getWhiteAgent());
			}
		}		
	}

	/**
	 * @return the comboBox
	 */
	public JComboBox getComboBox() {
		return comboBox;
	}

	/**
	 * @param comboBox the comboBox to set
	 */
	public void setComboBox(JComboBox comboBox) {
		this.comboBox = comboBox;
	}

	/**
	 * @return the agentColor
	 */
	public AgentColor getAgentColor() {
		return agentColor;
	}

	/**
	 * @param agentColor the agentColor to set
	 */
	private void setAgentColor(AgentColor agentColor) {
		this.agentColor = agentColor;
	}

}
